package com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.bo.ITXingzhengfawenNeirongBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.FaWenInfo;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class XingZhengFlowGenZongAction extends Action{
	private ITXingzhengfawenNeirongBO xingzhengfawenNeirongBO;
	
	public ITXingzhengfawenNeirongBO getXingzhengfawenNeirongBO() {
		return xingzhengfawenNeirongBO;
	}

	public void setXingzhengfawenNeirongBO(
			ITXingzhengfawenNeirongBO xingzhengfawenNeirongBO) {
		this.xingzhengfawenNeirongBO = xingzhengfawenNeirongBO;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		Integer first =  0;
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		String biaoti = request.getParameter("biaoti");
		String wentype = request.getParameter("wentype");
		String zhuangtai = request.getParameter("zhuangtai");
		String zhuangta = request.getParameter("zhuangta");
		String fawenwenhao = request.getParameter("fawenwenhao");
		String starttime=request.getParameter("starttime");
		String endtime=request.getParameter("endtime");
		List<FaWenInfo> faWenInfoList = new ArrayList<FaWenInfo>();
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		if (request.getParameter("start") == null) {
			faWenInfoList = xingzhengfawenNeirongBO.xingZhengFlowGengZong( 0, (0 + 23), biaoti, wentype, zhuangtai,fawenwenhao,starttime,endtime,zhuangta);

		} else {
			 first = Integer.parseInt(request.getParameter("start"));
			faWenInfoList =  xingzhengfawenNeirongBO.xingZhengFlowGengZong( (first + 1), (first + 23) , biaoti,  wentype, zhuangtai,fawenwenhao,starttime,endtime,zhuangta);

			
		}
		
		Integer totalProperty = xingzhengfawenNeirongBO.xingZhengFlowGengZongCount( (first + 1), (first + 23) , biaoti,  wentype, zhuangtai,fawenwenhao,starttime,endtime,zhuangta);
		object.put("totalProperty", totalProperty);
		for (FaWenInfo faWenInfo : faWenInfoList) {
			if(faWenInfo.getId() != null){
				JSONObject object2 = new JSONObject(faWenInfo);
				if(faWenInfo.getZhuangta().equals("0")){
					String operateStr="<a href='"+basePath+"documentationflow/xingzhengfawen/genzong.jsp?flowid="+faWenInfo.getFlowId()+"'>跟踪</a>" 
								+ "&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/xingzhengfawen/shenpi.do?method=selectonedoc&id="+faWenInfo.getId()+"'>查看</a>"
								+ "&nbsp;&nbsp;<a href='"+basePath+"documentationflow/xingzhengfawen/fubenlist.jsp?id="+faWenInfo.getId()+"'>查看副本</a>";
						if (workFlowTemplate.isSuspend(Long.valueOf(faWenInfo.getFlowId()) )) {  		//中止状态
							operateStr += "&nbsp;&nbsp;<a href='javascript:resume("+faWenInfo.getFlowId()+")'>重启</a>";
							object2.put("isend", "stop");
						} else if("结束".equals(faWenInfo.getZhuangtai())){  		//结束状态
							object2.put("isend", "end");
						}else{   				//流程中各状态
							operateStr += "&nbsp;&nbsp;<a href='javascript:suspend("+faWenInfo.getFlowId()+")'>中止</a>";
							object2.put("isend", "run");
						}
						operateStr+= "&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/xingzhengfawen/shenpi.do?method=updatedocform&id="+faWenInfo.getId()+"'>修改正文</a>"
							+ "&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/xingzhengfawen/shenpi.do?method=updateform&id="+faWenInfo.getId()+"'>修改</a>"
							+ "&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/xingzhengfawen/shenpi.do?method=updatefujianform&id="+faWenInfo.getId()+"'>修改附件</a>";
						object2.put("operate",operateStr);
						array.put(object2);
				}
				if(faWenInfo.getZhuangta().equals("1")){
					String operateStr="<a href='"+basePath+"documentationflow/xingzhengfawen/genzong.jsp?flowid="+faWenInfo.getFlowId()+"'>跟踪</a>" 
						+ "&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/xingzhengfawen/shenpi.do?method=fengfaselectonedoc&id="+faWenInfo.getId()+"'>查看</a>"
						+ "&nbsp;&nbsp;<a href='"+basePath+"documentationflow/xingzhengfawen/fubenlist.jsp?id="+faWenInfo.getId()+"'>查看副本</a>";
					if (workFlowTemplate.isSuspend(Long.valueOf(faWenInfo.getFlowId()) )) {  		//中止状态
						operateStr += "&nbsp;&nbsp;<a href='javascript:resume("+faWenInfo.getFlowId()+")'>重启</a>";
						object2.put("isend", "stop");
					} else if("结束".equals(faWenInfo.getZhuangtai())){  		//结束状态
						object2.put("isend", "end");
					}else{   				//流程中各状态
						operateStr += "&nbsp;&nbsp;<a href='javascript:suspend("+faWenInfo.getFlowId()+")'>中止</a>";
						object2.put("isend", "run");
					}
					operateStr+= "&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/xingzhengfawen/shenpi.do?method=fengfaupdatedocform&id="+faWenInfo.getId()+"'>修改正文</a>"
						+ "&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/xingzhengfawen/shenpi.do?method=fengfaupdateform&id="+faWenInfo.getId()+"'>修改</a>"
						+ "&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/xingzhengfawen/shenpi.do?method=fengfaupdatefujianform&id="+faWenInfo.getId()+"'>修改附件</a>";
					object2.put("operate",operateStr);
					array.put(object2);
				}
			}
			
		}
		object.put("root", array);
		response.getWriter().print(object);
		return null;
	}
	
}
